//
//  WaitBuyderRefundVC.m
//  CenturyCity
//
//  Created by beisheng03 on 2018/1/18.
//  Copyright © 2018年 beisheng. All rights reserved.
//

#import "WaitBuyderRefundVC.h"
#import "MyInfoFooterView.h"
#import "ForRefundTopCell.h"
#import "WaitBuyderRefCell.h"
@interface WaitBuyderRefundVC ()
@property (nonatomic, strong) MyInfoFooterView *footerView;

@end

@implementation WaitBuyderRefundVC

- (ApplyRefundParmModel *)parmModel{
    if (!_parmModel) {
        _parmModel = [[ApplyRefundParmModel alloc]init];
    }
    
    
    return _parmModel;
}

- (MyInfoFooterView *)footerView{
    
    if (!_footerView) {
        
        MyInfoFooterView *temp = (MyInfoFooterView *)[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([MyInfoFooterView class]) owner:nil options:nil].firstObject;
        
        temp.bottomLabel.hidden = YES;
        
        temp.topBtn.backgroundColor = themeColor;
        
        temp.backgroundColor = viewBgGrey;
        
        [temp.topBtn setTitle:@"提 交" forState:UIControlStateNormal];
        
        [temp.topBtn addTarget:self action:@selector(tijiaoInfo) forControlEvents:UIControlEventTouchUpInside];
        
        _footerView = temp;
        
    }
    
    return _footerView;
}


- (void)tijiaoInfo{
    
    [self.view endEditing:YES];
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"order_id"] = self.parmModel.order_id;
    parameters[@"order_goods_id"] = self.parmModel.order_goods_id;
    
    if (self.parmModel.refund_shipping_company == nil) {
        
        [self showInformation:@"请填写物流公司"];
        
        return;
    }
    parameters[@"refund_shipping_company"] = self.parmModel.refund_shipping_company;

    
    if (self.parmModel.refund_shipping_code == nil) {
        
        [self showInformation:@"请填写运单号"];
        
        return;
    }
    
    parameters[@"refund_shipping_code"] = self.parmModel.refund_shipping_code;
    
    
    [SVProgressHUD showWithStatus:@"正在提交信息..."];
    MJWeakSelf;
    [[NetworkTools shareNetworkTools]sendRequestWithHttpMethod:POST_METHOD URLPath:@"Order/orderGoodsReturnGoods" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
            
            [SVProgressHUD showSuccessWithStatus:@"信息已提交!"];
            
            [[NSNotificationCenter defaultCenter]postNotificationName:@"DidPostWuLiuInfor" object:nil];
            
            [weakSelf performSelector:@selector(back) withObject:nil afterDelay:2.0];
            
            
        } else {
            
            [SVProgressHUD showInfoWithStatus:error.domain];
        }
        
        
    }];
    
    
}



- (void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:animated];
    
    
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
    
    
    
    
}
- (void)showInformation:(NSString *)str{
    
    UIAlertController *contr = [UIAlertController alertControllerWithTitle:nil message:str preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        
    }];
    
    
    //    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    //
    //
    //    }];
    
    
    //    [contr addAction:action2];
    [contr addAction:action1];
    
    [self presentViewController:contr animated:YES completion:nil];
    
    
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self setNav];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.backgroundColor = viewBgGrey;
    
//    [self.tableView registerClass:[BaseTableViewCell class] forCellReuseIdentifier:[BaseTableViewCell identifierStr]];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([ForRefundTopCell class]) bundle:nil] forCellReuseIdentifier:[ForRefundTopCell identifierStr]];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([WaitBuyderRefCell class]) bundle:nil] forCellReuseIdentifier:[WaitBuyderRefCell identifierStr]];

    
    self.tableView.tableFooterView = self.footerView;
    


}

- (void)back{
    
    
    
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)setNav{
    
    self.navigationItem.title = @"申请退款";
    UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
    [but setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateNormal];
    [but setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateHighlighted];
    
    [but addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    
    but.imageView.contentMode = UIViewContentModeScaleAspectFit;
    
    //        but.backgroundColor = [UIColor redColor];
    
    but.frame = CGRectMake(0, 0, 44, 44);
    but.contentEdgeInsets = UIEdgeInsetsMake(11, -10, 11, 22);
    
    UIBarButtonItem *returnButtonItem = [[UIBarButtonItem alloc]initWithCustomView:but];
    
    
    self.navigationItem.leftBarButtonItem = returnButtonItem;
}





#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    return 2;
    
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    if (indexPath.row == 0) {
        ForRefundTopCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[ForRefundTopCell identifierStr]];
        
        tableCell.model = self.parmModel;
        
        return tableCell;
    } else {
        WaitBuyderRefCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[WaitBuyderRefCell identifierStr]];
        
        tableCell.model = self.parmModel;
        
        return tableCell;
    }
    

    
    
    
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    if (indexPath.row == 0) {
        
        return 90;
        
    } else {
        
        NSString *temp = @"请退货,未经卖家同意请不要使用到付或平邮.退货地址:该商家暂未配置收货地址";
        NSMutableDictionary *tempDic = [[NSMutableDictionary alloc]init];
        tempDic[NSFontAttributeName] = MYFONT(15);
        tempDic[NSKernAttributeName] = @(1.0f);
        
        NSMutableParagraphStyle *para = [[NSMutableParagraphStyle alloc]init];
        para.lineSpacing = 1;
        para.firstLineHeadIndent = 20;
        tempDic[NSParagraphStyleAttributeName] = para;

      CGSize size = [temp boundingRectWithSize:CGSizeMake(KScreenW - 24, 0) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:tempDic context:nil].size;
        return 200 + size.height;
    }
    
    
    
   
    
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.view endEditing:YES];
}


- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
    
    [self.view endEditing:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
